<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\mobilenode\model;

class RoleAdmin extends \app\BaseModel
{
    protected $name = "massage_mobile_role_admin";
    protected $append = ["nickName"];
    public function getNickNameAttr($value, $data)
    {
        if(!empty($data["user_id"])) {
            $user_model = new \app\massage\model\User();
            $name = $user_model->where(["id" => $data["user_id"]])->value("nickName");
            return $name;
        }
    }
    public function getNodeAttr($value, $data)
    {
        if(!empty($value)) {
            return explode(",", $value);
        }
        return $value;
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("status desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function checkDatas($uniacid, $user_id, $admin_user)
    {
        $dis = ["uniacid" => $uniacid, "user_id" => $user_id];
        $find = $this->where($dis)->where("status", ">", -1)->find();
        if(!empty($find)) {
            return ["code" => 500, "msg" => "已经绑定过该用户"];
        }
        $p = new \app\mobilenode\info\PermissionMobilenode($uniacid);
        $auth_num = $p->getAuthNumber();
        $num = $this->where(["uniacid" => $uniacid])->where("status", ">", -1)->count();
        if($auth_num <= $num) {
            return ["code" => 500, "msg" => "授权数量不足" . $auth_num . "-" . $num];
        }
        $admin_model = new \app\massage\model\Admin();
        $phone_admin_num = $admin_model->where(["id" => $admin_user["admin_id"]])->value("phone_admin_num");
        if($admin_user["is_admin"] == 0) {
            $num = $this->where(["uniacid" => $uniacid, "admin_id" => $admin_user["admin_id"]])->where("status", ">", -1)->count();
            if($phone_admin_num <= $num) {
                return ["code" => 500, "msg" => "代理商授权权限数量不足"];
            }
        }
        return true;
    }
    public function checkAuthData($data)
    {
        $dis = ["status" => 1, "user_id" => $data["id"]];
        $find = $this->dataInfo($dis);
        if(!empty($find["admin_id"])) {
            $admin_model = new \app\massage\model\Admin();
            $admin_where = ["id" => $find["admin_id"], "status" => 1, "agent_coach_auth" => 1];
            $find = $admin_model->dataInfo($admin_where);
        }
        $arr["mobilenode_auth"] = !empty($find) ? 1 : 0;
        return $arr;
    }
}

?>